package com.xudow.app.ui.task;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;

import com.activeshare.edu.ucenter.common.messages.AgencyMessage;
import com.activeshare.edu.ucenter.common.messages.cart.CartMessage;
import com.activeshare.edu.ucenter.models.base.Agencies;
import com.activeshare.edu.ucenter.models.cart.ShoppingCartWithCourse;
import com.xudow.app.Config;
import com.xudow.app.XApplication;
import com.xudow.app.model.CartModle;
import com.xudow.app.model.Coures;
import com.xudow.app.util.rest.XRestTemplate;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import static com.xudow.app.util.LogUtils.LOGE;
import static com.xudow.app.util.LogUtils.makeLogTag;

/**
 * 获取机构列表Task
 *
 * @author lukecheng
 */
public class GetAgencyListTask extends AsyncTask<HashMap<String, String>, Void, Void> {
    private static final String TAG = makeLogTag(GetAgencyListTask.class);

    private static final String AGENCY_LIST_URL = Config.BASE_URL + Config.OA_AGENCY_LIST_URL;

    public static final int SUCCESS = 0;
    public static final int ERROR = 1;

    private Context context;
    private Handler handler;

    public GetAgencyListTask(Context context, Handler handler) {
        this.context = context;
        this.handler = handler;
    }

    private void sendErrorMessage(String msg) {
        android.os.Message message = new android.os.Message();
        message.what = ERROR;

        if (null != msg) {
            Bundle data = new Bundle();
            data.putString("errMessage", msg);
            message.setData(data);
        }

        handler.sendMessage(message);
    }

    /**
     * 更改使用message.obj传递数据
      * @param list
     */
    private void sendSuccessMessage(List<Agencies> list) {
        android.os.Message message = new android.os.Message();
        message.what = SUCCESS;
        message.obj=list;
        handler.sendMessage(message);
    }

    @Override
    protected Void doInBackground(HashMap<String, String>... parameters) {
      XApplication application = (XApplication) context.getApplicationContext();

        XRestTemplate restTemplate = new XRestTemplate(application.getCookieStore(), context);
        restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());

        AgencyMessage result=restTemplate.getForObject(AGENCY_LIST_URL, AgencyMessage.class);
            if (result != null) {

                if (result.getResult().equals("0")){
                    sendSuccessMessage(result.getAgencyItems());
                }else if (result.getResult().equals("1")){
                    sendErrorMessage(result.getErrorMessage());
                }
            } else {
                sendErrorMessage(null);
            }
        return null;
    }


}
